package com.ruoyi.ctcx.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ctcx.domain.*;
import com.ruoyi.ctcx.domain.ro.InsuranceParam;
import com.ruoyi.ctcx.domain.ro.InsuranceQueryParam;
import com.ruoyi.ctcx.domain.vo.TbInsuranceContractDetailVo;
import com.ruoyi.ctcx.domain.vo.TbInsuranceContractMoneyVo;
import com.ruoyi.ctcx.domain.vo.TbInsuranceContractPayVo;
import com.ruoyi.ctcx.domain.vo.TbInsuranceContractQueryVo;
import com.ruoyi.ctcx.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 保险合同主Controller
 * 
 * @author ruoyi
 * @date 2024-11-19
 */
@RestController
@RequestMapping("/insurance/contract")
public class TbInsuranceContractController extends BaseControllerExt
{
    @Autowired
    private ITbInsuranceContractService tbInsuranceContractService;

    @Autowired
    private ITbInsuranceAttachmentService tbInsuranceAttachmentService;

    @Autowired
    private ITbInsuranceContractPremiumPlanService tbInsuranceContractPremiumPlanService;

    @Autowired
    private ITbInsuranceContractPremiumRealService tbInsuranceContractPremiumRealService;

    @Autowired
    private ITbInsuranceContractCommissionPlanService tbInsuranceContractCommissionPlanService;

    @Autowired
    private ITbInsuranceContractCommissionRealService tbInsuranceContractCommissionRealService;

    /**
     * 查询保险合同主列表
     */
    @PreAuthorize("@ss.hasPermi('insurance:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsuranceParam insuranceParam)
    {
        initDeptId(insuranceParam);
        startPage();
        List<TbInsuranceContractQueryVo> list = tbInsuranceContractService.getContractList(insuranceParam);
        return getDataTable(list);
    }

    /**
     * 导出保险合同主列表
     */
    @PreAuthorize("@ss.hasPermi('insurance:contract:export')")
    @Log(title = "保险合同主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InsuranceParam insuranceParam)
    {
        initDeptId(insuranceParam);
        List<TbInsuranceContractQueryVo> list = tbInsuranceContractService.getContractList(insuranceParam);
        ExcelUtil<TbInsuranceContractQueryVo> util = new ExcelUtil<TbInsuranceContractQueryVo>(TbInsuranceContractQueryVo.class);
        util.exportExcel(response, list, "保险合同主数据");
    }

    /**
     * 获取保险合同主详细信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tbInsuranceContractService.selectTbInsuranceContractById(id));
    }

    /**
     * 获取保险合同主详细信息
     */
    //@PreAuthorize("@ss.hasPermi('insurance:contract:query')")
    @GetMapping("/getContractDetail")
    public AjaxResult getContractDetail(InsuranceQueryParam insuranceQueryParam)
    {
        TbInsuranceContractDetailVo tbInsuranceContractDetailVo = tbInsuranceContractService.selectTbInsuranceContractByInsureId(insuranceQueryParam);
        if (null  != tbInsuranceContractDetailVo )
        {
            List<TbInsuranceAttachment> tbInsuranceAttachmentList = tbInsuranceAttachmentService.selectTbInsuranceAttachmentByMainId(tbInsuranceContractDetailVo.getMainId());

            if (null != tbInsuranceAttachmentList && !tbInsuranceAttachmentList.isEmpty())
            {
                tbInsuranceContractDetailVo.setAttachmentList(tbInsuranceAttachmentList);
            }
        }
        return success(tbInsuranceContractDetailVo);
    }

    /**
     * 保费缴纳信息列表
     */
    //@PreAuthorize("@ss.hasPermi('insurance:contract:query')")
    @GetMapping("/getContractPremium")
    public AjaxResult getContractPremium(InsuranceQueryParam insuranceQueryParam)
    {
        TbInsuranceContractMoneyVo vo = new TbInsuranceContractMoneyVo();
        TbInsuranceContractPremiumPlan qPlan =  new TbInsuranceContractPremiumPlan();
        qPlan.setInsureId(insuranceQueryParam.getInsureId());

        TbInsuranceContractPremiumReal qReal = new TbInsuranceContractPremiumReal();
        qReal.setInsureId(insuranceQueryParam.getInsureId());
        List<TbInsuranceContractPremiumPlan> premiumPlanList = tbInsuranceContractPremiumPlanService.selectTbInsuranceContractPremiumPlanList(qPlan);

        List<TbInsuranceContractPremiumReal> premiumRealList = tbInsuranceContractPremiumRealService.selectTbInsuranceContractPremiumRealList(qReal);

        if (null != premiumPlanList)
        {
            vo.setPremiumPlanList(premiumPlanList);
        }
        if (null != premiumRealList)
        {
            vo.setPremiumRealList(premiumRealList);
        }
        return success(vo);
    }

    /**
     * 代理佣金支付信息
     * @param insuranceQueryParam
     * @return
     */
    @GetMapping("/getContractPay")
    public AjaxResult getContractPay(InsuranceQueryParam insuranceQueryParam)
    {
        TbInsuranceContractPayVo vo = new TbInsuranceContractPayVo();
        TbInsuranceContractCommissionPlan qPlan =  new TbInsuranceContractCommissionPlan();
        qPlan.setInsureId(insuranceQueryParam.getInsureId());

        TbInsuranceContractCommissionReal qReal = new TbInsuranceContractCommissionReal();
        qReal.setInsureId(insuranceQueryParam.getInsureId());

        List<TbInsuranceContractCommissionPlan> commissionPlanList = tbInsuranceContractCommissionPlanService.selectTbInsuranceContractCommissionPlanList(qPlan);

        List<TbInsuranceContractCommissionReal> commissionRealList = tbInsuranceContractCommissionRealService.selectTbInsuranceContractCommissionRealList(qReal);

        if (null != commissionPlanList)
        {
            vo.setCommissionPlanList(commissionPlanList);
        }
        if (null != commissionRealList)
        {
            vo.setCommissionRealList(commissionRealList);
        }
        return success(vo);
    }


    /**
     * 新增保险合同主
     */
    @PreAuthorize("@ss.hasPermi('insurance:contract:add')")
    @Log(title = "保险合同主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbInsuranceContract tbInsuranceContract)
    {
        tbInsuranceContract.setCreateBy(getUsername());
        return toAjax(tbInsuranceContractService.insertTbInsuranceContract(tbInsuranceContract));
    }

    /**
     * 修改保险合同主
     */
    @PreAuthorize("@ss.hasPermi('insurance:contract:edit')")
    @Log(title = "保险合同主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbInsuranceContract tbInsuranceContract)
    {
        tbInsuranceContract.setUpdateBy(getUsername());
        return toAjax(tbInsuranceContractService.updateTbInsuranceContract(tbInsuranceContract));
    }

    /**
     * 删除保险合同主
     */
    @PreAuthorize("@ss.hasPermi('insurance:contract:remove')")
    @Log(title = "保险合同主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbInsuranceContractService.deleteTbInsuranceContractByIds(ids));
    }
}
